import request from '@/utils/request'

// 查询设备计划维护列表
export function listMtplan(query) {
  return request({
    url: '/sbtz/mtplan/list',
    method: 'get',
    params: query
  })
}

// 查询设备计划维护详细
export function getMtplan(id) {
  return request({
    url: '/sbtz/mtplan/' + id,
    method: 'get'
  })
}

// 新增设备计划维护
export function addMtplan(data) {
  return request({
    url: '/sbtz/mtplan',
    method: 'post',
    data: data
  })
}

// 修改设备计划维护
export function updateMtplan(data) {
  return request({
    url: '/sbtz/mtplan',
    method: 'put',
    data: data
  })
}

// 删除设备计划维护
// export function delMtplan(id) {
//   return request({
//     url: '/sbtz/mtplan/' + id,
//     method: 'delete'
//   })
// }

export function delMtplan(ids) {
  const path = Array.isArray(ids) ? ids.join(',') : ids
  return request({
    url: `/sbtz/mtplan/${path}`,
    method: 'delete'
  })
}

export function listPlansByAsset(assetId, params = {}) {
  return request({
    url: `/sbtz/mtplan/plan/${assetId}`,
    method: 'get',
    params
  })
} 

// 新增：创建计划
export function createPlan(data) {
  return request({
    url: '/sbtz/mtplan/createplan',
    method: 'post',
    data
  })
}

export function getPlanSpares(planId) {
  return request({
    url: `/sbtz/mtplan/${planId}/spares`,
    method: 'get'
  })
}

// 全量替换备件清单（先删后插）
export function replacePlanSpares(planId, spares) {
  // spares: [{spareId, qty}]
  return request({
    url: `/sbtz/mtplan/${planId}/spares`,
    method: 'put',
    data: spares
  })
} 
 

// 读取计划详情（需返回主表字段 + spares）
export function getPlanDetail(id) {
  return request({
    url: `/sbtz/mtplan/getdetail/${id}`,
    method: 'get'
  })
}

// 更新主表字段（标题/日期/内容/needSpare/status 等）
export function updatePlan(id, data) {
  return request({
    url: `/sbtz/mtplan/updateplan/${id}`,
    method: 'put',
    data
  })
}


export function listMtplanSpareHistory({ assetId, pageNum = 1, pageSize = 20, keyword, dateRange, planId }) {
  const params = {
    pageNum, pageSize,
    keyword: keyword || null,
    planId: planId || null,
    'params[beginTime]': Array.isArray(dateRange) ? dateRange[0] : null,
    'params[endTime]':   Array.isArray(dateRange) ? dateRange[1] : null,
  }
  return request({
    url: `/sbtz/mtplan/history/${assetId}`,
    method: 'get',
    params
  })
}
 
export function replacePlanSpecialSpares(id, items) {
  return request({
    url: `/sbtz/mtplan/${id}/spares/special`,
    method: 'put',
    data: items  
  })
}

 
